import Vue from 'vue'
import Vuex from 'vuex'
import {index,appLogin,getSkinType,wechatShare} from '@/api/api.js'
import { isQianfan ,getDeviceId,getLocation, isWeiXin} from '../until/QFH5'
const wxModule = require('jweixin-module')

Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		request_host: '',//七牛提交host
		AppNavBarHeight:{px:0,rpx:0},//状态栏
		qiniuUrl:'https://qiance.qianfanyun.com/guess',//qiniu地址
		siteinfo:{},
		shareInfo:{},//二次分享
		loginInfo:{},//登录信息
		serviceInfo:{},//客服信息
		guide:{
			isWechat:false,//微信
			phone:"",//手机号
			platText:"",//平台
		},//引导弹窗
		guideShow:0,//引导弹窗
		deviceId:'',//设备号,
		activityName:'',//活动名称
		activityType:'',//活动类型
		taskVideoStatus:false,
		step:'index',//接力步骤
		relayData:{},//接力基础数据
		basicData:{},//基础数据
		formRelayData:{},
		posterData:{},/*海报数据*/
		mainColor:'',//主题色
		startMove:true,//是否在滚动，
		updateTask:false,
		race_id:-1,
		skinType:null,// 代表默认的皮肤
		
		OpenMusic:false,//音乐动画控制
		MusicObj:null,//全局音乐
		
		frameData:null,//注册全局frame接受参数
		
		showCollect:false,//收集用户信息
		showCollectPhone:'',//收集的号码
		
		long: '0', //经度
		lat: '0', //纬度
	},
	mutations: {
		setHost(state, host){
			state.request_host = host;
		},
		SETAPPNAVBARHEIGHT(state, data){
			state.AppNavBarHeight = data
		},
		SETQINIUURL(state, data){
			state.qiniuUrl = data
		},
		SETSITEINFO(state, data){
			state.siteinfo = data
		},
		SETSHARE(state,data){
			state.shareInfo = data
		},
		SETLOGIN(state,data){
			state.loginInfo = data
		},
		SETSERVICE(state,data){
			state.serviceInfo = data
		},
		SETGUIDE(state,data){
			state.guide = data
		},
		SETGUIDEWECHAT(state,data){
			state.guide.isWechat = data
		},
		SETGUIDEPHONE(state,data){
			state.guide.phone = data
		},
		SETGUIDEPLATTEXT(state,data){
			state.guide.platText = data
		},
		SETGUIDESHOW(state,data){
			state.guideShow = data
		},
		SETDEVICEID(state,data){
			state.deviceId = data
		},
		SETACTIVITYTYPE(state,data){
			state.activityType = data
		},
		SETACTIVITYNAME(state,data){
			state.activityName = data
		},
		SETTASKVIDEOSTATUS(state,data){
			state.taskVideoStatus = data
		},
		SET_RELAY_DATA(state,data){
			state.relayData = data
		},
		SET_BASIC_DATA(state,data){
			state.basicData = data
		},
		SET_FORM_RELAY_DATA(state,data){
			state.formRelayData = data
		},
		SET_MAIN_COLOR(state,data){
			state.mainColor = data
		},
		SET_START_MOVE(state,data){
			state.startMove = data
		},
		SETUPDATETASK(state,data){
			state.updateTask = data
		},
		SET_RACE_ID(state,data){
			state.race_id = data
		},
		SET_SKINTYPE(state,data){
			state.skinType = data
		},
		SET_MUSIC_OPEN(state,data){
			state.OpenMusic = data
		},
		SET_MUSIC(state,data){
			state.MusicObj = data
		},
		SET_FRAMEDATA(state,data){
			state.frameData = data
		},
		SET_SHOWCOLLECT(state,data){
			state.showCollect = data
		},
		SET_SHOWCOLLECTPHONE(state,data){
			state.showCollectPhone = data
		},
		updateLong(state, long) {
			state.long = long
		},
		updateLat(state, lat) {
			state.lat = lat
		},
	},
	getters: {
		AppNavBarHeight:state=>state.AppNavBarHeight,
		qiniuUrl:state=>state.qiniuUrl,
		siteinfo:state=>state.siteinfo,
		shareInfo:state=>state.shareInfo,
		loginInfo:state=>state.loginInfo,
		serviceInfo:state=>state.serviceInfo,
		guide:state=>state.guide,
		guideShow:state=>state.guideShow,
		isWechat:state=>state.guide.isWechat,
		activityName:state=>state.activityName,
		activityType:state=>state.activityType,
		taskVideoStatus:state=>state.taskVideoStatus,
		updateTask:state=>state.updateTask,
		skinType:state=>state.skinType,
		OpenMusic:state=>state.OpenMusic,
		MusicObj:state=>state.MusicObj,
		frameData:state=>state.frameData,
		showCollect:state=>state.showCollect,
		showCollectPhone:state=>state.showCollectPhone
	},
	actions: {
		//navBar高度
		setNavBarHeight({state,commit},data){
			commit('SETAPPNAVBARHEIGHT',data)
		},
		//七牛下载地址
		setQiniuUrl({state,commit},data){
			commit('SETQINIUURL',data)
		},
		//设置站点信息
		setSiteinfo({state,commit},data){
			// let nowTime = Math.floor((new Date() / 1000))
			// let indexData = uni.getStorageSync('indexData') || {}
			// if(JSON.stringify(indexData) != "{}") {
			// 	indexData=JSON.parse(indexData)
			// }
			// if(indexData.hasOwnProperty('cache') && indexData.cache > nowTime){
			// 	commit('SETSITEINFO',indexData)
			// 	commit('SETQINIUURL',indexData.qiniu_url+'/guess')
			// }else{
				index().then(res=>{
					let siteinfo = res.data
					commit('SETSITEINFO',siteinfo)
					commit('SETQINIUURL',siteinfo.qiniu_url+'/guess')
					commit("SETTASKVIDEOSTATUS",data)
					// siteinfo.cache = nowTime+siteinfo.cache_time
					uni.setStorageSync('indexData',JSON.stringify(res.data))
				})
			// }
		},
		//设置引导
		setGuide({state,commit},data){
			commit('SETGUIDE',data)
		},
		//是否绑定微信
		setGuideWechat({state,commit},data){
			commit('SETGUIDEWECHAT',data)
		},
		//是否绑定手机号
		setGuidePhone({state,commit},data){
			commit('SETGUIDEPHONE',data)
		},
		//平台引导
		setGuidePlatText({state,commit},data){
			commit('SETGUIDEPLATTEXT',data)
		},
		//设置引导弹窗
		setGuideShow({state,commit},data){
			commit('SETGUIDESHOW',data)
		},
		//设置分享信息
		setShareInfo({state,commit},data){
			commit("SETSHARE",data)
		},
		//app登录信息
		setLoginInfo({state,commit},data){
			// commit('SETGUIDEPHONE',data.phone)
			commit("SETLOGIN",data)
		},
		//客服信息
		serviceInfo({state,commit},data){
			commit("SETSERVICE",data)
		},
		//每次进入强制检查  app.vue 执行
		checkWechat({state,commit}){
			appLogin().then(res=>{
				let actid = uni.getStorageSync('actid') || null
				let token = uni.getStorageSync(actid+'bearerToken') || null
				console.log('执行结束',res)
				if(res.code==0){
					commit('SETGUIDEWECHAT',true)
					// if(token==null){
						uni.setStorageSync(actid+'bearerToken',res.data.token)
						uni.setStorageSync(actid+'bearerToken_expire',res.data.expire+Date.parse(new Date())/1000)
					// }

				}else if(res.code=='10012'){
					commit('SETGUIDEWECHAT',false)
				}else{
					// uni.showToast({title:res.text, icon:'none',duration:4000}) 
				}
			})
		},
		setDeviceid(){
			// console.log('设备号',res)
			// console.log('123')
			//state.deviceId = data

			// QFH5.getDeviceId(function(state,data){
			//   if(state==1){
			//     //获取设备号成功
			//     deviceid = data.deviceid;//用户设备唯一ID并MD5加密 string
			//   }
			// })

			// let res = getDeviceId()
			// console.log('设备号',res)
			// alert(JSON.stringify(res))
		},
		setActivityName({state,commit},data){
			commit("SETACTIVITYNAME",data)
		},
		setActivityType({state,commit},data){
			commit("SETACTIVITYTYPE",data)
		},
		setTaskVideoStatus({state,commit},data){
			commit("SETTASKVIDEOSTATUS",data)
		},
		//设置皮肤
		setSkinType({state,commit},data){
			// getSkinType({actid:data}).then(res=>{
			// 	let {skin_id} = res.data
			// 	let skin =skin_id==0?null:skin_id
			// 	commit("SET_SKINTYPE",skin)
			// })
			commit("SET_SKINTYPE",data)
		},
		createMusic({state,commit},data){
			commit('SET_MUSIC',data)
		},
		//注冊一個frame响应
		setIframeData({state,commit},data){
			window.addEventListener('message',function(e){
				console.log('接收到了',e.data)
				let data = e.data
				//强制兼容后台数据
				let frameData={ code:0, data:data }
				commit('SET_FRAMEDATA',frameData)
			})
				
		},
		getPos({state,commit}){
					if(isQianfan()){
					  getLocation(false,null)
					}else if(isWeiXin()){
						let url = window.location.href
						let postData = {}
						postData.url =  url
						postData.actid = uni.getStorageSync('actid') || ''
						wechatShare(postData).then(function (res) {
							let data = res.data
							wxModule.config(data)
								wxModule.getLocation({
									type: 'gcj02',
									success:function(res){
										commit('updateLong', res.longitude)
										commit('updateLat', res.latitude)
									},
									cancel: function () {
								        uni.showToast({
											title:'定位失败,请刷新页面获取并允许定位',
											icon:'none'
								        }) ;
								    },
								    fail: function (e) {
										uni.showToast({
											title:'定位失败,请检刷新页面后重新尝试'+JSON.stringify(e),
											icon:'none'
										}) ;
								    }
								})
						 
					    })
					} else{
					  if(navigator.geolocation) {
						  navigator.geolocation.getCurrentPosition(
							  function (position) {  
								commit('updateLong', position.coords.longitude)
								commit('updateLat', position.coords.latitude)  
							  },
							  function (e) {
								commit('updateLong', '0')
								commit('updateLat', '0')
								throw(e.message);
							  }
						  ) 
					  }
					}
				  }
	}
})

export default store
